package com.yc.snacknet_vue.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.yc.snacknet_vue.bean.MemberInfo;
import com.yc.snacknet_vue.dao.DBHelper;
import com.yc.snacknet_vue.dao.IMemberInfoDAO;
import com.yc.snacknet_vue.utils.StringUtil;

/**
 * 会员数据库 模型层实现
 * company 源辰信息
 * @author 38929
 * @date 2021年8月6日
 * @version 1.0
 * Email 1198865589@qq.com
 */
public class MemberInfoDAOImpl implements IMemberInfoDAO {

	@Override
	public MemberInfo login(MemberInfo mem) {
		DBHelper db = new DBHelper();
		String sql = "select mno, nickName, realName, tel, email, photo from memberinfo where"
				+ " status != 0  and (nickName = ? or tel = ? or email = ?) and pwd = md5(?)";
		return db.findSingle(MemberInfo.class, sql, mem.getNickName(), mem.getNickName(), mem.getNickName(), mem.getPwd());
	}
	
	/**
	 * 多条件分页查询
	 * @param t
	 * @param page  当前页码数
	 * @param rows  每页显示数量
	 * @return
	 */
	public List<MemberInfo> finds(MemberInfo mem,int page, int rows) {
		DBHelper db = new DBHelper();
		StringBuffer sb =  new StringBuffer();
		sb.append("select mno, nickName, IFNull(realName,'') realName, tel, email, "
				+ "date_format(regDate, '%Y-%m-%d %H:%i:%s') regDate, status from  memberinfo where 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if(mem != null) {
			if( mem.getMno() != null){
				sb.append(" and mno = ? ");
				params.add(mem.getMno());
			}
			//通过昵称 模糊查询
			if(!StringUtil.checkNull(mem.getNickName())) {
				sb.append(" and nickName like concat('%', ?, '%') ");
				params.add(mem.getNickName());
			}
			if(!StringUtil.checkNull(mem.getTel())) {
				sb.append(" and tel = ? ");
				params.add(mem.getTel());
			}
		}
		
		sb.append(" order by mno desc limit ?, ? ");
		params.add((page-1) * rows);
		params.add(rows);
		
		if( null == params || params.isEmpty()) {
			return db.findMultiple(MemberInfo.class, sb.toString());
		}
		return db.findMultiple(MemberInfo.class, sb.toString(), params.toArray());
	}

	/**
	 * 条件查询会员总数
	 * @return
	 */
	public int totals(MemberInfo mem) {
		DBHelper db = new DBHelper();
		StringBuffer sb =  new StringBuffer();
		sb.append("select count(mno) from  memberinfo where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if(null == mem) {
			return  db.getTotal(sb.toString());	
		}
		if( mem.getMno() != null){
			sb.append(" and mno = ? ");
			params.add(mem.getMno());
		}
		//通过昵称 模糊查询
		if(!StringUtil.checkNull(mem.getNickName())) {
			sb.append(" and nickName like concat('%', ?, '%') ");
			params.add(mem.getNickName());
		}
		if(!StringUtil.checkNull(mem.getTel())) {
			sb.append(" and tel = ? ");
			params.add(mem.getTel());
		}
		if( null != params && !params.isEmpty()) {
			return db.getTotal(sb.toString(), params.toArray());
		}
		return db.getTotal(sb.toString());
	}

	//添加用户
	@Override
	public int add(MemberInfo mem) {
		DBHelper db = new DBHelper();
		String sql = "insert into memberinfo values(0,?, '', md5(?), ?, ?, '', now(), 1)";
		return db.update(sql, mem.getMno(),mem.getPwd(),mem.getTel(),mem.getEmail());
	}

	//修改用户密码
	@Override
	public int update(String mno) {
		DBHelper db = new DBHelper();
		String sql = "update memberinfo set pwd = md5(right(tel,6)) where mno = ?";
		return db.update(sql, mno);
	}

	//修改用户状态
	@Override
	public int delete(String status, String mno) {
		DBHelper db = new DBHelper();
		String sql = "update memberinfo set status = ? where mno = ?";
		return db.update(sql, status, mno);
	}

	//用户注册
	@Override
	public int reg(String nickName, String pwd, String email, String tel) {
		DBHelper db = new DBHelper();
//INSERT INTO memberinfo VALUES ('1', 'navy', '', 'c8837b23ff8aaa8a2dde915473ce0991', '15096098010', '278224975@qq.com', '', '2020-05-16 15:32:15', '1');
		String sql = "insert into memberinfo values (null, ?, '', md5(?), ?, ?, '', now(), '1')";
		return db.update(sql, nickName, pwd, tel, email);
	}
}
